public class BinarySearchTree {
        class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
            public TreeNode(int val) {
            this.val = val;
        }
    }
    private TreeNode root;
    public boolean search(int val) {
        TreeNode cur = root;
        while(cur!=null) {
            if(val > cur.val) {
                cur = cur.right;
            } else if( val < cur.val){
                cur = cur.left;
            } else {
                return true;
            }
        }
        return false;
    }
    public void insert(int val) {
        TreeNode cur = root;
        TreeNode precur = cur;
        if(root == null) {
            root = new TreeNode(val);
            return;
        }
        while(cur != null) {

             precur = cur;
            if(cur.val > val) {
                cur = cur.left;
            } else if(cur.val < val) {
                cur = cur.right;
            } else{
                System.out.println("插入失败");
                return;
            }
        }
        if(precur.val > val) {
            precur.left = new TreeNode(val);
        }else {
            precur.right = new TreeNode(val);
        }
    }
}
